package cn.lkk.samples.lang.classes.CustomeClasses;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 这个程序用于演示如何床架你一个自定义类。
 * 1. 一个源文件中只能有一个同名的 public 类，但是可以有任意多个非 public 类
 */
public class EmployeeTest {
    public static void main(String[] args) {

        // 创建一个 Employee 数组并填充
        Employee[] staff = new Employee[3];
        staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
        staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
        staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

        // 给每个员工增加5%
        for (Employee e : staff)
            e.raiseSalary(5);

        // 打印每个员工的信息
        for (Employee e : staff)
            System.out.println("name=" + e.getName() + " ,salary=" + e.getSalary() + " ,hireDay=" + e.getHireDay());
    }
}

//2.非 public 类可以有任意多个
class Employee {

    //3. final字段必须保证在任意构造函数调用之后有值
    private final String name;
    //4. 类中的字段都应该是private类型的
    private double salary;
    private Date hireDay;

    public Employee(String n, double s, int year, int month, int day) {
        name = n;
        salary = s;
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        hireDay = calendar.getTime();
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public Date getHireDay() {
        return hireDay;
    }

    public void raiseSalary(double byPercent) {
        //5. this 关键字代表隐式参数
        double raise = this.salary * byPercent / 100;
        this.salary += raise;
    }

    public boolean equals(Employee obj) {
        //6. 可以直接访问同类其他对象的private成员
        return name.equals(obj.name);
    }
}
